package com.chen.androidstudy

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.content.res.Resources
import com.base.library.BaseApplication
import com.base.netlib.NetLib
import com.chen.androidstudy.constant.Constants
import com.chen.androidstudy.utils.CommonUtil
import com.chen.androidstudy.utils.CrashCollectHandler
import com.chen.androidstudy.utils.SystemUtil
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.bugly.crashreport.CrashReport.CrashHandleCallback
import com.tencent.bugly.crashreport.CrashReport.UserStrategy
import com.tencent.mmkv.MMKV
import com.chen.androidstudy.utils.TimeUtil
import dagger.hilt.android.HiltAndroidApp


@HiltAndroidApp
class App : BaseApplication() {
    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var instance: App
    }

    override fun getLogTag() = "Ordering"

    override fun onCreate() {
        super.onCreate()
        instance = this
//        setTheme(R.style.Theme_OrderingSys)
        CrashCollectHandler.instance.init(this)
        initBugly()
        initNet()
        MMKV.initialize(this)
    }

    private fun initBugly() {
        val strategy = UserStrategy(this)
        strategy.setCrashHandleCallback(object : CrashHandleCallback() {
            /**
             * Crash处理.
             *
             * @param crashType 错误类型：CRASHTYPE_JAVA，CRASHTYPE_NATIVE，CRASHTYPE_U3D ,CRASHTYPE_ANR
             * @param errorType 错误的类型名
             * @param errorMessage 错误的消息
             * @param errorStack 错误的堆栈
             * @return 返回额外的自定义信息上报
             */
            override fun onCrashHandleStart(
                crashType: Int, errorType: String?,
                errorMessage: String?, errorStack: String?
            ): MutableMap<String, String?> {
                CrashReport.setDeviceId(context, CommonUtil.getUniqueDeviceId())
                CrashReport.setUserId("")
                CrashReport.putUserData(this@App, "staffName", "")
                CrashReport.putUserData(this@App, "token", "")
                return mutableMapOf(
                    Pair(
                        "staffName",
                        "name1"
                    ),
                    Pair(
                        "token",
                        "123"
                    )
                )
            }
        })
//        CrashReport.initCrashReport(
//            applicationContext,
//            BuildConfig.BUGLY_APP_ID,
//            false,
//            strategy
//        )
    }


    /**
     * 初始化网络相关
     */
    private fun initNet() {
        NetLib.config(this).setBaseUrl(Constants.HOST_URL).addHeaders {
            return@addHeaders mapOf(
                Pair(
                    Constants.HEADER_DEVICE, SystemUtil.getDeviceId(instance)
                ), Pair(
                    Constants.HEADER_TS, TimeUtil.genTimeStampSeconds().toString()
                ), Pair(
                    Constants.HEADER_DEVICE_ID, CommonUtil.getUniqueDeviceId()
                ), Pair(
                    Constants.HEADER_VERSION, BuildConfig.VERSION_NAME
                ), Pair(
                    Constants.HEADER_TOKEN, ""
                )
            )
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1f) //非默认值
            resources
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources? {
        val res = super.getResources()
        if (res.configuration.fontScale != 1f) { //非默认值
            val newConfig = Configuration()
            newConfig.setToDefaults() //设置默认
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }
}